package jwei.apps;

import jwei.apps.adapters.StylesCursorAdapter;
import jwei.apps.db.StyleCache.StylesCache;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;

public class ShoppingCartActivity extends ListActivity {

    private StylesCursorAdapter scA;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shopping_cart_layout);

        Cursor c = getContentResolver().query(StylesCache.CONTENT_URI, null, StylesCache.IS_QUEUE + " = 1", null, null);
        startManagingCursor(c);
        Log.i("ShoppingCartActivity", "QUERIED " + c.getCount() + " STYLES FROM CACHE");
        String[] from = new String[] { StylesCache.PRODUCT_NAME, StylesCache.BRAND_NAME };
        int[] to = new int[] { R.id.product_name, R.id.brand_name };

        // create cursor adapter
        scA = new StylesCursorAdapter(ShoppingCartActivity.this, R.layout.product_list_entry, c, from, to);
        setListAdapter(scA);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        if (scA == null) { return; }

        Cursor c = (Cursor) scA.getItem(position);

        Intent i = new Intent(ShoppingCartActivity.this, ProductDetailsActivity.class);
        long productId = c.getLong(c.getColumnIndex(StylesCache.PRODUCT_ID));
        i.putExtra("productId", productId);
        i.putExtra("productName", c.getString(c.getColumnIndex(StylesCache.PRODUCT_NAME)));
        i.putExtra("productUrl", c.getString(c.getColumnIndex(StylesCache.PRODUCT_URL)));
        i.putExtra("brandName", c.getString(c.getColumnIndex(StylesCache.BRAND_NAME)));
        i.putExtra("imageUrl", c.getString(c.getColumnIndex(StylesCache.IMAGE_URL)));
        i.putExtra("styleId", c.getLong(c.getColumnIndex(StylesCache.STYLE_ID)));
        i.putExtra("price", c.getString(c.getColumnIndex(StylesCache.PRICE)));
        startActivity(i);
    }

}
